<template>
  <div class="contain-report">
    <div class="report-title">教师管理</div>
    <div class="report-search">
      <el-input
          size="small"
          style="width: 200px;margin-right: 20px"
          placeholder="请输入手机号码"
          clearable
          @clear="getList"
          v-model="form.phone"
      >
      </el-input>
      <el-input
          size="small"
          style="width: 200px;margin-right: 20px"
          placeholder="请输入用户名"
          clearable
          @clear="getList"
          v-model="form.userName"
      >
      </el-input>
      <el-button type="primary" size="small" @click="searchList">查 询</el-button>
      <el-button size="small" @click="resetForm">重 置</el-button>
    </div>
    <div class="report-btns">
      <el-button type="primary" size="small" @click="downloadFile" style="margin-right: 20px">模板下载</el-button>
      <el-upload
          class="upload-demo"
          :action=uploadUrl
          :headers=myHeaders
          ref="upload"
          :on-success="leadingIn"
          :limit="1"
          :data="uploadData"
          :show-file-list="false"
      >
        <el-button size="small">批量导入</el-button>
      </el-upload>
      <el-button type="success" size="small" @click="addDialog = true" style="margin-left: 20px">新 建</el-button>
    </div>
    <div class="report-table">
      <el-table
          :data="tableData"
          style="width: 100%">
        <el-table-column label="手机号" align="center" prop="phone">
        </el-table-column>
        <el-table-column label="账号" align="center" prop="studentNumber">
        </el-table-column>
        <el-table-column label="姓名" align="center" prop="userName">
        </el-table-column>
        <el-table-column label="负责班级数" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.classIdList ? scope.row.classIdList.length : '暂无班级' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="负责实验室数" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.laboratoryId ? scope.row.laboratoryId.length : '暂无实验室' }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="openEdit(scope.row)">编 辑</el-button>
            <el-button type="text" @click="deleteStudent(scope.row.id)">删 除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="report-page">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="1"
          :page-sizes="[5]"
          :page-size="5"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <el-dialog
        :show-close="false"
        :visible.sync="addDialog"
        width="40%"
    >
      <div class="dialog-box">
        <div class="box-top">新建教师</div>
        <div class="box-body">
          <el-form label-width="80px">
            <el-form-item label="姓名:">
              <el-input placeholder="请输入姓名" v-model="addForm.userName"></el-input>
            </el-form-item>
            <el-form-item label="手机号:">
              <el-input placeholder="请输入手机号" v-model="addForm.phone"></el-input>
            </el-form-item>
            <el-form-item label="账号:">
              <el-input placeholder="请输入账号" v-model="addForm.studentNumber"></el-input>
            </el-form-item>
            <el-form-item label="班级:">
              <el-select placeholder="请选择班级" multiple v-model="addForm.classIds" style="width: 100%">
                <el-option
                    v-for="item in classData"
                    :key="item.id"
                    :label="item.className"
                    :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="实验室:">
              <el-select placeholder="请选择实验室" multiple v-model="addForm.laboratoryIds" style="width: 100%">
                <el-option
                    v-for="item in laboratory"
                    :key="item.id"
                    :label="item.laboratoryName"
                    :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div class="form-btn">
            <el-button type="primary" size="small" @click="submitAdd" style="margin-right: 20px">提 交</el-button>
            <el-button size="small" @click="addDialog = false">取 消</el-button>
          </div>
        </div>
      </div>
    </el-dialog>
    <el-dialog
        :show-close="false"
        :visible.sync="editDialog"
        width="40%"
    >
      <div class="dialog-box">
        <div class="box-top">编辑教师</div>
        <div class="box-body">
          <el-form label-width="80px">
            <el-form-item label="姓名:">
              <el-input placeholder="请输入姓名" v-model="editForm.userName"></el-input>
            </el-form-item>
            <el-form-item label="手机号:">
              <el-input placeholder="请输入手机号" v-model="editForm.phone"></el-input>
            </el-form-item>
            <el-form-item label="账号:">
              <el-input placeholder="请输入账号" v-model="editForm.studentNumber"></el-input>
            </el-form-item>
            <el-form-item label="班级:">
              <el-select placeholder="请选择班级" multiple v-model="editForm.classIdList" style="width: 100%">
                <el-option
                    v-for="item in classData"
                    :key="item.id"
                    :label="item.className"
                    :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="实验室:">
              <el-select placeholder="请选择实验室" multiple v-model="editForm.laboratoryId" style="width: 100%">
                <el-option
                    v-for="item in laboratory"
                    :key="item.id"
                    :label="item.laboratoryName"
                    :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div class="form-btn">
            <el-button type="primary" size="small" @click="submitEdit" style="margin-right: 20px">提 交</el-button>
            <el-button size="small" @click="editDialog = false">取 消</el-button>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {classList, deleteUser, laboratoryList, searchUserList, addTeacher, editTeacher} from "../../api/laboratory";
import request from "../../utils/request";
import common from "../../utils/common";

export default {
  name: "userManagement",
  data() {
    return {
      uploadUrl: common.requestUrl + '/user/upload',
      myHeaders: {
        token: ''
      },
      uploadData: {
        status: 2
      },
      form: {
        pageIndex: 1,
        pageSize: 5,
        phone: "",
        roleFlag: 2,
        userName: ""
      },
      classData: [],
      laboratory: [],
      tableData: [],
      total: null,
      addDialog: false,
      addForm: {
        classIds: [],
        laboratoryIds: [],
        phone: "",
        studentNumber: "",
        userName: ""
      },
      editDialog: false,
      editForm: {}
    }
  },
  created() {
    this.myHeaders.token = sessionStorage.getItem('token')
  },
  mounted() {
    this.getClassList()
    this.getLaboratory()
    this.getList()
  },
  methods: {
    leadingIn(res) {
      console.log(res)
      if (res.code !== 200) {
        this.$message.error(res.message)
      } else {
        this.$message.success(res.message)
        this.getList()
      }
      this.$refs.upload.clearFiles() //上传成功之后清除历史记录
    },
    downloadFile() {
      request({
        url: common.requestUrl + "/upload/downloadTemplate",
        method: 'get',
        responseType: 'blob',
      }).then(resp => {
        console.log('ppp', resp)
        const blob = new Blob([resp.data], {type: "application/vnd.ms-excel"});
        const url = window.URL.createObjectURL(blob);
        let a = document.createElement('a');
        a.download = '教师模板.xls';
        a.href = url;
        a.click();
      })  //导出文件请求
    },
    async getClassList() {
      const {data} = await classList()
      if (data.code === 200) {
        this.classData = data.data.data
      }
    },
    //查询实验室
    async getLaboratory() {
      const {data} = await laboratoryList()
      if (data.code === 200) {
        this.laboratory = data.data
      }
    },
    async getList() {
      const {data} = await searchUserList(this.form)
      console.log(data)
      if (data.code === 200) {
        this.tableData = data.data.data
        this.total = data.data.total
      }
    },
    searchList() {
      if (!this.form.phone && !this.form.userName) {
        this.$message.error('请至少填写一位参数')
      } else {
        this.getList()
      }
    },
    resetForm() {
      this.form.phone = ''
      this.form.userName = ''
      this.getList()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.form.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.form.pageIndex = val
      this.getList()
    },
    deleteStudent(id) {
      this.$confirm('此操作将永久删除该教师, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUser(id).then(res => {
          if (res.data.code === 200) {
            this.$message.success(res.data.message)
            this.getList()
          }
        })
      })
    },
    async submitAdd() {
      console.log(this.addForm)
      for (let key in this.addForm) {
        if (!this.addForm[key]) {
          return this.$message.error('请见参数填写完整')
        }
      }
      const {data} = await addTeacher(this.addForm)
      if (data.code === 200) {
        this.$message.success(data.message)
        await this.getList()
        this.addForm = {}
        this.addDialog = false
      }
    },
    openEdit(item) {
      console.log(item)
      this.editForm = item
      this.editDialog = true
    },
    async submitEdit() {
      const {userName, phone, studentNumber, classIdList, laboratoryId, id} = this.editForm
      const edit = {
        userName,
        phone,
        studentNumber,
        userId: id,
        classIds: classIdList,
        laboratoryIds: laboratoryId
      }
      // for (let key in edit){
      //     if (!edit[key]){
      //         return this.$message.error('请将编辑参数填写完整')
      //  }
      // }
      const {data} = await editTeacher(edit)
      if (data.code === 200) {
        this.$message.success(data.message)
        await this.getList()
        this.editDialog = false
        this.editForm = {}
      }
    }
  }
}
</script>

<style scoped lang="less">
.contain-report {
  position: relative;
  width: 100%;
  height: 100%;
  padding: 20px;
  box-sizing: border-box;

  .dialog-box {
    width: 100%;
    min-height: 50vh;
    border-radius: 5px;
    overflow: hidden;

    .box-top {
      width: 100%;
      height: 10vh;
      background-image: linear-gradient(to left, #948af1 0%, #c193db 50%, #ed9bc6 100%);
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 26px;
      color: #fff;
    }

    .box-body {
      width: 100%;
      height: 80%;
      padding: 5% 10%;
      box-sizing: border-box;
      background-color: #ffffff;

      /deep/ .el-input__inner {
        border: none;
        background-color: #ecedef;
      }

      /deep/ .el-textarea__inner {
        border: none;
        background-color: #ecedef;
      }

      .form-btn {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .file-list {
        margin-top: 20px;
        width: 100%;

        .file-box {
          margin-top: 10px;
          width: 100%;
          display: flex;
          align-items: center;
          justify-content: space-between;

          .left {
            width: 40%;
            display: flex;
            align-items: center;
            justify-content: space-between;
          }
        }
      }
    }
  }

  .report-title {
    font-size: 22px;
    color: #666;
  }

  .report-search {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-end;

    p {
      margin: 0 10px;
      box-sizing: border-box;

      &:hover {
        cursor: pointer;
      }
    }
  }

  .report-btns {
    width: 100%;
    display: flex;
    align-items: center;
  }

  .report-table {
    width: 100%;
    margin: 20px 0;
    box-sizing: border-box;
  }

  .report-page {
    position: absolute;
    bottom: 10px;
    left: 50%;
    width: 100%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>
